gave LinkBatch its own file
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 # These are used in incrementalSetup()
12 define ('LINKCACHE_GOOD', 0);
13 define ('LINKCACHE_BAD', 1);
14 define ('LINKCACHE_IMAGE', 2);
15 define ('LINKCACHE_PAGE', 3);
16
17 /**
18 * @package MediaWiki
19 * @subpackage Cache
20 */
21 class LinkCache {
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
25
26 /* private */ var $mPageLinks;
27 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
28 /* private */ var $mImageLinks, $mCategoryLinks;
29 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
30 /* private */ var $mForUpdate;
31
32 /* private */ function getKey( $title ) {
33 global $wgDBname;
34 return $wgDBname.':lc:title:'.$title;
35 }
36
37 function LinkCache() {
38 $this->mActive = true;
39 $this->mPreFilled = false;
40 $this->mForUpdate = false;
41 $this->mPageLinks = array();
42 $this->mGoodLinks = array();
43 $this->mBadLinks = array();
44 $this->mImageLinks = array();
45 $this->mCategoryLinks = array();
46 $this->mOldGoodLinks = array();
47 $this->mOldBadLinks = array();
48 $this->mOldPageLinks = array();
49 }
50
51 /**
52 * General accessor to get/set whether SELECT FOR UPDATE should be used
53 */
54 function forUpdate( $update = NULL ) {
55 return wfSetVar( $this->mForUpdate, $update );
56 }
57
58 function getGoodLinkID( $title ) {
59 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
60 return $this->mGoodLinks[$title];
61 } else {
62 return 0;
63 }
64 }
65
66 function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks );
68 }
69
70 function addGoodLinkObj( $id, $title ) {
71 if ( $this->mActive ) {
72 $dbkey = $title->getPrefixedDbKey();
73 $this->mGoodLinks[$dbkey] = $id;
74 $this->mPageLinks[$dbkey] = $title;
75 }
76 }
77
78 function addBadLinkObj( $title ) {
79 $dbkey = $title->getPrefixedDbKey();
80 if ( $this->mActive && ( ! $this->isBadLink( $dbkey ) ) ) {
81 $this->mBadLinks[$dbkey] = 1;
82 $this->mPageLinks[$dbkey] = $title;
83 }
84 }
85
86 function addImageLink( $title ) {
87 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
88 }
89
90 function addImageLinkObj( $nt ) {
91 if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
92 }
93
94 function addCategoryLink( $title, $sortkey ) {
95 if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
96 }
97
98 function addCategoryLinkObj( &$nt, $sortkey ) {
99 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
100 }
101
102 function clearBadLink( $title ) {
103 unset( $this->mBadLinks[$title] );
104 $this->clearLink( $title );
105 }
106
107 function clearLink( $title ) {
108 global $wgMemc, $wgLinkCacheMemcached;
109 if( $wgLinkCacheMemcached )
110 $wgMemc->delete( $this->getKey( $title ) );
111 }
112
113 /** @deprecated */
114 function suspend() { $this->mActive = false; }
115 /** @deprecated */
116 function resume() { $this->mActive = true; }
117
118 function getPageLinks() { return $this->mPageLinks; }
119 function getGoodLinks() { return $this->mGoodLinks; }
120 function getBadLinks() { return array_keys( $this->mBadLinks ); }
121 function getImageLinks() { return $this->mImageLinks; }
122 function getCategoryLinks() { return $this->mCategoryLinks; }
123
124 /**
125 * Add a title to the link cache, return the page_id or zero if non-existent
126 * @param string $title Title to add
127 * @return integer
128 */
129 function addLink( $title ) {
130 $nt = Title::newFromDBkey( $title );
131 if( $nt ) {
132 return $this->addLinkObj( $nt );
133 } else {
134 return 0;
135 }
136 }
137
138 /**
139 * Add a title to the link cache, return the page_id or zero if non-existent
140 * @param Title $nt Title to add
141 * @return integer
142 */
143 function addLinkObj( &$nt ) {
144 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
145 $title = $nt->getPrefixedDBkey();
146 if ( $this->isBadLink( $title ) ) { return 0; }
147 $id = $this->getGoodLinkID( $title );
148 if ( 0 != $id ) { return $id; }
149
150 $fname = 'LinkCache::addLinkObj';
151 global $wgProfiling, $wgProfiler;
152 if ( $wgProfiling && isset( $wgProfiler ) ) {
153 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
154 }
155
156 wfProfileIn( $fname );
157
158 $ns = $nt->getNamespace();
159 $t = $nt->getDBkey();
160
161 if ( '' == $title ) {
162 wfProfileOut( $fname );
163 return 0;
164 }
165
166 $id = NULL;
167 if( $wgLinkCacheMemcached )
168 $id = $wgMemc->get( $key = $this->getKey( $title ) );
169 if( ! is_integer( $id ) ) {
170 if ( $this->mForUpdate ) {
171 $db =& wfGetDB( DB_MASTER );
172 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
173 $options = array( 'FOR UPDATE' );
174 } else {
175 $options = array();
176 }
177 } else {
178 $db =& wfGetDB( DB_SLAVE );
179 $options = array();
180 }
181
182 $id = $db->selectField( 'page', 'page_id',
183 array( 'page_namespace' => $ns, 'page_title' => $t ),
184 $fname, $options );
185 if ( !$id ) {
186 $id = 0;
187 }
188 if( $wgLinkCacheMemcached )
189 $wgMemc->add( $key, $id, 3600*24 );
190 }
191
192 if( 0 == $id ) {
193 $this->addBadLinkObj( $nt );
194 } else {
195 $this->addGoodLinkObj( $id, $nt );
196 }
197 wfProfileOut( $fname );
198 return $id;
199 }
200
201 /**
202 * Bulk-check the pagelinks and page arrays for existence info.
203 * @param Title $fromtitle
204 * @deprecated
205 */
206 function preFill( &$fromtitle ) {
207 global $wgAntiLockFlags;
208 $fname = 'LinkCache::preFill';
209 wfProfileIn( $fname );
210
211 $this->suspend();
212 $id = $fromtitle->getArticleID();
213 $this->resume();
214
215 if( $id == 0 ) {
216 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
217 wfProfileOut( $fname );
218 return;
219 }
220
221 if ( $this->mForUpdate ) {
222 $db =& wfGetDB( DB_MASTER );
223 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
224 $options = 'FOR UPDATE';
225 } else {
226 $options = '';
227 }
228 } else {
229 $db =& wfGetDB( DB_SLAVE );
230 $options = '';
231 }
232
233 $page = $db->tableName( 'page' );
234 $pagelinks = $db->tableName( 'pagelinks' );
235
236 $sql = "SELECT page_id,pl_namespace,pl_title
237 FROM $pagelinks
238 LEFT JOIN $page
239 ON pl_namespace=page_namespace AND pl_title=page_title
240 WHERE pl_from=$id $options";
241 $res = $db->query( $sql, $fname );
242 while( $s = $db->fetchObject( $res ) ) {
243 $title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
244 if( $s->page_id ) {
245 $this->addGoodLinkObj( $s->page_id, $title );
246 } else {
247 $this->addBadLinkObj( $title );
248 }
249 }
250 $this->mPreFilled = true;
251
252 wfProfileOut( $fname );
253 }
254
255 /**
256 * Clears cache
257 */
258 function clear() {
259 $this->mPageLinks = array();
260 $this->mGoodLinks = array();
261 $this->mBadLinks = array();
262 $this->mImageLinks = array();
263 $this->mCategoryLinks = array();
264 $this->mOldGoodLinks = array();
265 $this->mOldBadLinks = array();
266 $this->mOldPageLinks = array();
267 }
268
269 /**
270 * Swaps old and current link registers
271 * @deprecated
272 */
273 function swapRegisters() {
274 swap( $this->mGoodLinks, $this->mOldGoodLinks );
275 swap( $this->mBadLinks, $this->mOldBadLinks );
276 swap( $this->mImageLinks, $this->mOldImageLinks );
277 swap( $this->mPageLinks, $this->mOldPageLinks );
278 }
279 }
280 ?>